﻿package com.game.view.layer.loadingBar
{
	import com.game.data.constant.ConfigConst;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class LoadingBar3 extends LoadingBarBase
	{
		
		public function LoadingBar3():void
		{
			super();
			x = 0;
			y = 0;
		}
		
		private var bg:Sprite;
		private var frame:Sprite;
		private var barBg:Sprite;
		private var bar:Sprite;
		private var tips:TextField;
		private var timer:Timer;
		
		override protected function setProcess(data:Object):void
		{
			bar.graphics.clear();
			bar.graphics.beginFill(0x00FF00, 1);
			bar.graphics.drawRoundRect(330, 320, (data.curPercent / 100) * 280, 13, 20, 20);
			bar.graphics.endFill();
		}
		
		override protected function beforeAdded():void
		{
			bg = new Sprite();
			bg.x = bg.y = 0;
			bg.graphics.beginFill(0, 0.2);
			bg.graphics.drawRect(0, 0, 940, 590);
			bg.graphics.endFill();
			
			frame = new Sprite();
			frame.x = frame.y = 0;
			frame.graphics.beginFill(0, 0.8);
			frame.graphics.drawRoundRect(320, 250, 300, 90, 30, 30);
			frame.graphics.endFill();
			
			barBg = new Sprite();
			barBg.x = barBg.y = 0;
			barBg.graphics.beginFill(0xFF9900, 1);
			barBg.graphics.drawRoundRect(330, 320, 280, 13, 20, 20);
			barBg.graphics.endFill();
			
			bar = new Sprite();
			bar.x = bar.y = 0;
			
			tips = new TextField();
			tips.selectable = false;
			tips.width = 940;
			tips.height = 21;
			tips.x = 0;
			tips.y = 290;
			tips.embedFonts = true;
			var tf:TextFormat = new TextFormat();
			tf.font = ConfigConst.GAME_CONFIG_FONT;
			tf.size = 15;
			tf.color = 0xFFFFFF;
			tf.align = "center";
			tips.defaultTextFormat = tf;
			tips.appendText("正在加载资源，请稍等");
			
			timer = new Timer(500, 0);
			timer.addEventListener(TimerEvent.TIMER, refreshTxt);
			timer.start();
		}
		
		override protected function onAdded():void
		{
			super.onAdded();
			addChild(bg);
			addChild(frame);
			addChild(barBg);
			addChild(bar);
			addChild(tips);
		}
		
		override protected function beforeRemoved():void
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, refreshTxt);
			timer = null;
		}
		
		private function refreshTxt(param1:TimerEvent):void
		{
			var idx:int = tips.text.indexOf("......");
			if (idx == -1)
			{
				tips.appendText(".");
			}
			else
			{
				tips.text = tips.text.slice(0, idx);
			}
		}
		
		override public function dispose():void
		{
			removeFromParent();
		}
	}
}